﻿
#include "MoveData.h"

#include <QCoreApplication>


MoveData::MoveData(JistDocument *document, int index, Direction direction, JistTableView *tableView):
    m_pDocument(document),
    m_Index(index),
    m_Direction(direction),
    m_pTableView(tableView)
{
    setText((direction == Down) ?
            QCoreApplication::translate("Undo Commands", "Down list") :
            QCoreApplication::translate("Undo Commands", "Up list"));
}

void MoveData::redo()
{
    moveListData(m_Direction);
}

void MoveData::undo()
{
    moveListData((m_Direction == Down) ? Up : Down);
}

void MoveData::moveListData(Direction dir)
{
    // 테스트
    //m_ModelIndexList = m_pTableView->selectedIndexes();
    //QRect r = m_ModelIndexList.value(0).data(Qt::UserRole).toRect();
    const int currentIndex = m_pTableView->m_iSelectIndex;//currentIndex().row();
    int moveIndex = (dir == Down) ? currentIndex + 1 : currentIndex - 1;
    // 레이어 설정
    int aniId = 0;
    if(m_pDocument->m_AnimaionTableWidget){
        aniId = m_pDocument->m_AnimaionTableWidget->m_iSelectIndex;
    }
    switch(m_pTableView->m_eChangeMode)
    {

        case JistTableView::IMGLIST_CHANGE:
        {
             m_pDocument->m_ImageData.swap(currentIndex, moveIndex);
             m_pDocument->m_SpriteData[currentIndex].swap(m_pDocument->m_SpriteData[moveIndex]);
            break;
        }
        case JistTableView::SPRITELIST_CHANGE:
        {
             m_pDocument->m_SpriteData[m_pDocument->m_iCurrentSpriteDataIndex].swap(currentIndex, moveIndex);
            break;
        }
        case JistTableView::ANIMATIONLIST_CHANGE:
        {
            //int moveIndex = (dir == Down) ? currentIndex + 1 : currentIndex - 1;
            // 셀리스트뷰에 대한 갱신
            QList< QVector< QList< JistCommonData > > > tempCellList = m_pDocument->m_CellData.toList();
            tempCellList.swap(currentIndex, moveIndex );
            m_pDocument->m_CellData = tempCellList.toVector();
            m_pDocument->m_CellListModel.setDataList(&m_pDocument->m_CellData[moveIndex][m_pDocument->m_iCurrentCellDataIndex]);
            m_pDocument->m_CellTableWidget->refineView(0);
            // 신리스트뷰에 대한 갱신
            QList< QList < JistCommonData> > tempList = m_pDocument->m_SceneData.toList();
            tempList.swap(currentIndex, moveIndex );
            m_pDocument->m_SceneData = tempList.toVector();
            m_pDocument->m_SceneListModel.setDataList(&m_pDocument->m_SceneData[moveIndex]);
            m_pTableView->refineViewNoUndo(moveIndex);
            // 애니메이션에 이동에의한 셀레이어 순서 재설정
            int changeIndex[2] = {currentIndex, moveIndex};
            for(int a = 0; a <2; a++){
                for(int s = 0; s < m_pDocument->m_CellData[changeIndex[a]].count(); s++){
                    for(int c = 0; c < m_pDocument->m_CellData[changeIndex[a]][s].count(); c++){

                        {
                            m_pDocument->m_CellData[changeIndex[a]][s][c].m_pIItem->setZValue(changeIndex[a] + c * 0.0001f);
                           (m_pDocument->m_CellData[changeIndex[a]][s][c]).m_pIItem->recalculateResult();
                        }
                    }
                }
            }

            break;
        }
        case JistTableView::SCENELIST_CHANGE:
        {
            //int moveIndex = (dir == Down) ? currentIndex + 1 : currentIndex - 1;
            // 셀리스트뷰에 대한 갱신
            //스왑을 이용하기 위해 리스트로 변환
            QList< QList< JistCommonData > >  tempCellList = m_pDocument->m_CellData[m_pDocument->m_iCurrentSceneDataIndex].toList();
            //신리스트에 신을 새로 추가하게 되면 상하 이동안됨 - 셀리스트를 생성해야만 이동가능
            //if(tempCellList.at(currentIndex).isEmpty() || tempCellList.at(moveIndex).isEmpty())
            //    return;
            tempCellList.swap(currentIndex, moveIndex );
            m_pDocument->m_CellData[m_pDocument->m_iCurrentSceneDataIndex] = tempCellList.toVector();
            m_pDocument->m_CellListModel.setDataList(&m_pDocument->m_CellData[m_pDocument->m_iCurrentSceneDataIndex][moveIndex]);
            //m_pDocument->m_CellTableWidget->refineView(0);
            break;
        }
    }

    const bool selectedBefore = (m_Index == currentIndex);
    const int prevIndex = m_pTableView->currentIndex().row();

//    JistCommonModel *dataModel = (JistCommonModel*)(m_pTableView->model());
//    JistCommonData data = dataModel->takeAt(m_Index);



//    // Change the direction and index to swap undo/redo
    m_Index = (dir == Down) ? m_Index + 1 : m_Index - 1;
//    //m_Direction = (m_Direction == Down) ? Up : Down;

    const bool selectedAfter = (m_Index == currentIndex);
    int selectIndex = (selectedBefore ? m_Index :(selectedAfter ? prevIndex : currentIndex));
//    if(data.m_pIItem){
//        data.m_pIItem->setZValue(aniId + selectIndex * 0.0001f);
//        data.m_pIItem->setId(selectIndex);
//    }
//    dataModel->insertData(m_Index, &data);

//    // 레이어 설정
//    if(data.m_pIItem){
//        dataModel->m_pList->value(prevIndex).m_pIItem->setZValue(aniId + prevIndex * 0.0001f);
//        dataModel->m_pList->value(prevIndex).m_pIItem->setId(prevIndex);
//    }
    m_pTableView->refineViewNoUndo(selectIndex);
    // 스프라이트 이동에 의한 갱신
    switch(m_pTableView->m_eChangeMode)
    {
        case JistTableView::IMGLIST_CHANGE:
        {

            for(int a = 0; a < m_pDocument->m_CellData.count(); a++){
                for(int s = 0; s < m_pDocument->m_CellData[a].count(); s++){
                    for(int c = 0; c < m_pDocument->m_CellData[a][s].count(); c++){
                        int imgId = (m_pDocument->m_CellData[a][s][c]).m_iImgIndex;
                        if(currentIndex == imgId)
                            (m_pDocument->m_CellData[a][s][c]).m_iImgIndex = m_Index;
                        else if(m_Index == imgId)
                            (m_pDocument->m_CellData[a][s][c]).m_iImgIndex = currentIndex;
                        int spriteId = (m_pDocument->m_CellData[a][s][c]).m_iSpriteIndex;
                    }
                }
            }
            // 원더런 맵툴 대응
            m_pTableView->SyncObjectImgIndex(currentIndex, moveIndex);
            break;
        }

        case JistTableView::SPRITELIST_CHANGE:
        {
            for(int a = 0; a < m_pDocument->m_CellData.count(); a++){
                for(int s = 0; s < m_pDocument->m_CellData[a].count(); s++){
                    for(int c = 0; c < m_pDocument->m_CellData[a][s].count(); c++){
                        int imgId = (m_pDocument->m_CellData[a][s][c]).m_iImgIndex;
                        int spriteId = (m_pDocument->m_CellData[a][s][c]).m_iSpriteIndex;
        //                if(imgId == m_pDocument->m_iCurrentSpriteDataIndex
        //                        && spriteId == currentIndex)
                        {
                            // 셀리스트의 갱신
                            if(imgId != -1 && spriteId != -1 && imgId < m_pDocument->m_SpriteData.count() && spriteId < m_pDocument->m_SpriteData[imgId].count())
                                (m_pDocument->m_CellData[a][s][c]).m_img = m_pDocument->m_SpriteData[imgId][spriteId].m_img;
                            // 빈 셀
                            else
                                (m_pDocument->m_CellData[a][s][c]).m_img = g_EmptyImage;
                            // 신뷰의 갱신
                            qreal r = (m_pDocument->m_CellData[a][s][c]).m_pIItem->scale();
                            QSizeF sc = (m_pDocument->m_CellData[a][s][c]).m_pIItem->scaleXY();
                            (m_pDocument->m_CellData[a][s][c]).m_pIItem->init();
                            (m_pDocument->m_CellData[a][s][c]).m_pIItem->setScaleX(sc.width());
                            (m_pDocument->m_CellData[a][s][c]).m_pIItem->setScaleY(sc.height());
                            (m_pDocument->m_CellData[a][s][c]).m_pIItem->recalculateResult();
                        }
                    }
                }
            }
            // 원더런 맵툴 대응
            m_pTableView->SwapSprite(currentIndex, moveIndex);
            if(m_pDocument->m_CellTableWidget)
                m_pDocument->m_CellTableWidget->refineViewNoUndo(m_pDocument->m_CellTableWidget->currentIndex().row());
            break;
        }
    }

}
